package com.lanxiao.cloud.modules.gateway.route.controller;

import com.lanxiao.cloud.common.core.dto.PageQuery;
import com.lanxiao.cloud.common.core.dto.ResultModel;
import com.lanxiao.cloud.common.core.enums.OperationType;
import com.lanxiao.cloud.common.web.invalid.Log;
import com.lanxiao.cloud.modules.gateway.route.application.GatewayRouteCmdManager;
import com.lanxiao.cloud.modules.gateway.route.application.GatewayRouteQueryManager;
import com.lanxiao.cloud.modules.gateway.route.application.dto.GatewayRouteCmdAdd;
import com.lanxiao.cloud.modules.gateway.route.application.dto.GatewayRouteCmdUpdate;
import com.lanxiao.cloud.modules.gateway.route.application.dto.GatewayRoutePageDto;
import com.lanxiao.cloud.modules.gateway.route.application.dto.GatewayRouteQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author ：qiaoliang
 * @date ：2021-08-19
 */
@Api(tags = "网关路由服务")
@Validated
@RequestMapping("/gateway-routes")
@RestController
public class GatewayRouteController {

  private final GatewayRouteCmdManager cmdManager;
  private final GatewayRouteQueryManager queryManager;

  public GatewayRouteController(
      GatewayRouteCmdManager cmdManager,
      GatewayRouteQueryManager queryManager) {
    this.cmdManager = cmdManager;
    this.queryManager = queryManager;
  }

  @ApiOperation(value = "分页查询网关路由")
  @GetMapping
  public ResultModel<GatewayRoutePageDto> query(GatewayRouteQuery query, PageQuery page){
    return queryManager.query(query, page);
  }

  @Log(operationType = OperationType.CREATE)
  @ApiOperation(value = "新增网关路由")
  @PostMapping
  public ResponseEntity<?> save(@Valid @RequestBody GatewayRouteCmdAdd commandAdd){
    cmdManager.save(commandAdd);
    return ResponseEntity.status(HttpStatus.CREATED).build();
  }

  @Log(operationType = OperationType.UPDATE)
  @ApiOperation(value = "修改网关路由")
  @PutMapping("/{id}")
  public ResponseEntity<?> update(@PathVariable Long id,
      @RequestBody GatewayRouteCmdUpdate commandUpdate){
    cmdManager.update(id, commandUpdate);
    return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).build();
  }

  @Log(operationType = OperationType.DELETE)
  @ApiOperation(value = "删除网关路由")
  @DeleteMapping("/{id}")
  public ResponseEntity<?> delete(@PathVariable Long id){
    cmdManager.delete(id);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }

}
